/**
 * 
 */
(function() {
    'use strict';

    angular
        .module('milkbeeApp')
        .controller('GroupMonthController', GroupMonthController);

    GroupMonthController.$inject = ['$scope','$filter', '$state','$http', 'GroupMonth', 'ParseLinks', 'AlertService', 'paginationConstants', 'pagingParams'];

    function GroupMonthController ($scope,$filter, $state, $http,GroupMonth, ParseLinks, AlertService, paginationConstants, pagingParams) {
    	 var vm = this;

         vm.loadPage = loadPage;
         vm.predicate = pagingParams.predicate;
         vm.reverse = pagingParams.ascending;
         vm.transition = transition;
         vm.itemsPerPage = paginationConstants.itemsPerPage;
         
         vm.openCalendar = openCalendar;
         vm.datePickerOpenStatus = {};
         vm.startDate=null;
         vm.xAxisData=[];
         vm.cardCounts=[];
         vm.lightCounts=[];
         vm.mychart={};
         loadAll();
         
         function loadAll () {
        	 GroupMonth.query({
        		 startDate:pagingParams.startDate
             }, onSuccess, onError);

             function onSuccess(data, headers) {
                 vm.monthGroupResults = data;         
                 vm.startDate=pagingParams.startDate;
                 
                 refreshChart();
             }
             function onError(error) {
                 AlertService.error(error.data.message);
             }
         }

         function loadPage(page) {      
             vm.transition();
         }

         function transition() {
             $state.transitionTo($state.$current, {
                 search: vm.currentSearch,
                 startDate:vm.startDate
             });
         }
         vm.datePickerOpenStatus.startDate=false;
         function openCalendar (date,picker) {
             vm.datePickerOpenStatus[date] = true;
             vm[picker].open=true;
         }  
           
         vm.picker1 = {
        	        //date: null,
        	        datepickerOptions: {
        	        	mode: 'month',
        	            minMode: 'month',
        	            maxMode: 'year'
        	        }
         };
         function initCharts(){
        	 vm.mychart = echarts.init(document.getElementById('mycanvas'));

             // 指定图表的配置项和数据
             var option = {
                 title: {
                     text: ''
                 },
                 tooltip: {},
                 legend: {
                     data:[]
                 },
                 xAxis: {
                     data: vm.xAxisData
                 },
                 yAxis: {},
                 series: [{
                     name: '',
                     type: 'bar',
                     data: vm.cardCounts
                 },{
                     name: '',
                     type: 'bar',
                     data: vm.lightCounts
                 }]
             };

             // 使用刚指定的配置项和数据显示图表。
             vm.mychart.setOption(option);
         }
         function refreshChart(){
        	 vm.cardCounts=[];
        	 vm.lightCounts=[];
        	 for (var i=0;i<vm.monthGroupResults.length;i++){
        		 vm.cardCounts[i]=vm.monthGroupResults[i].cardCount;
        		 vm.lightCounts[i]=vm.monthGroupResults[i].lightCount;
        	 }
        	 var option = {
                     title: {
                         text: ''
                     },
                     tooltip: {},
                     legend: {
                         data:[]
                     },
                     xAxis: {
                         data: vm.xAxisData
                     },
                     yAxis: {},
                     series: [{
                         name: '',
                         type: 'bar',
                         data: vm.cardCounts
                     },{
                         name: '',
                         type: 'bar',
                         data: vm.lightCounts
                     }]
                 };
        	 vm.mychart.setOption(option);
         }
         initCharts();
    }
})();
